package br.elsa.survey.view.beans.managed.tests;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.List;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import br.elsa.survey.dao.exceptions.DataNotFoundException;
import br.elsa.survey.domain.questions.Question;
import br.elsa.survey.services.QuestionService;
import br.elsa.survey.view.beans.managed.QuestionsManagementView;
import br.elsa.survey.view.messages.ViewMessages;

/**
 * TDD class for {@link QuestionsManagementView} class
 * 
 * @author diego
 * @since 2010-10-20
 */
public class QuestionsManagementViewTest {

	private QuestionsManagementView questionsManagementView;

	@Mock
	private QuestionService questionService;

	@Mock
	private ViewMessages viewMessages;

	@Before
	public void setUp() {
		MockitoAnnotations.initMocks(this);
		questionsManagementView = new QuestionsManagementView(questionService, viewMessages);
	}

	@Test
	public void loadQuestionsShouldCallQuestionsServiceAndSaveTheResultInFieldQuestions() throws DataNotFoundException {

		List<Question> mockedQuestions = mock(List.class);

		when(questionService.getAllQuestions()).thenReturn(mockedQuestions);

		questionsManagementView.loadQuestions();

		List<Question> questions = questionsManagementView.getQuestions();

		Assert.assertEquals(mockedQuestions, questions);
		verify(questionService, times(1)).getAllQuestions();
	}

	@Test
	public void listShouldAddInfoMessageInViewIfNoQuestionIsFound() throws DataNotFoundException {
		when(questionService.getAllQuestions()).thenThrow(new DataNotFoundException());

		questionsManagementView.loadQuestions();
		verify(viewMessages, times(1)).addWarnMessage(anyString());
	}

	@Test
	public void removeShouldCallQuestionServiceToRemoveQuestionAndPutInfoMessageIntoViewInThisOrder() {

		Question question = mock(Question.class);
		InOrder inOrder = inOrder(questionService, viewMessages);

		questionsManagementView.removeQuestion(question);

		inOrder.verify(questionService, times(1)).remove(question);
		inOrder.verify(viewMessages, times(1)).addInfoMessage(anyString());
	}
}
